package com.vacation.vacationdemo.intercept;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 杨光
 * @date 2022年03月12日 9:32
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        HttpSession session = request.getSession();

        Object loginUser = session.getAttribute("loginUser");
        //如果loginUser==null说明没有登录过
        if(loginUser==null){
            response.sendRedirect("/vacation/tologin");
            System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
            return false;
        }

        //放行
        return true;
//        request.getRequestDispatcher("/vacation/tologin").forward(request,response);

    }
}
